package com.yuwenxin.dao.impl;

import com.yuwenxin.bean.Combination;
import com.yuwenxin.bean.Musician;
import com.yuwenxin.core.BaseDaoImpl;
import com.yuwenxin.core.BaseQueryModel;
import com.yuwenxin.dao.CombinationDao;
import com.yuwenxin.dao.MusicianDao;

import java.sql.ResultSet;
import java.util.Date;
import java.util.HashSet;

public class MusicianDaoImpl extends BaseDaoImpl implements MusicianDao {
    @Override
    public String getSelectAllSql() {
        return "select * from musician";
    }

    @Override
    public String getSelectByNameSql() {
        return "select * from musician where singerName=?";
    }

    @Override
    public String getFuzzyFindSql() {
        return "select * from combination where singerName like ?";
    }

    @Override
    public String getUpdateSql(Object object) {
        return null;
    }

    @Override
    public String getDeleteSql() {
        return "delete from musician where idsinger=?";
    }

    @Override
    public String getSelectByIdSql() {
        return "select * from musician where idsinger=?";
    }

    @Override
    public String getInsertSql(Object object) {
        Musician _musician = (Musician) object;
        String singerName = _musician.getSingerName();
        String corporationName = _musician.getCorporationName();
        String singerPhotoPath = MusicianDao.DEFAULT_PATH;
        Date establishDate = _musician.getEstablishDate();
        int belongedCombinationid = _musician.getBelongedCombinationId();

        if (corporationName == null){
            return "insert into musician (singerName,singerPhotoPath,establishDate,belongedCombinationid) values('"
                    + singerName + "','" + singerPhotoPath + "','" + establishDate + "','" + belongedCombinationid + "')";
        }
        return "insert into musician (singerName,corporationName,singerPhotoPath,establishDate,belongedCombinationid) values('"
                + singerName + "','" + corporationName + "','" + singerPhotoPath + "','" + establishDate + "','" + belongedCombinationid + "')";
    }

    @Override
    public String getConditionSql(BaseQueryModel queryModel) {
        return null;
    }

    @Override
    public Object toEntity(ResultSet rs) throws Exception {
        Musician musician = new Musician();

        CombinationDao combinationDao = new CombinationDaoImpl();

        musician.setId(rs.getInt("idmusician"));
        musician.setSingerName(rs.getString("singerName"));
        musician.setCorporationName(rs.getString("corporationName"));
        musician.setSingerPhotoPath(rs.getString("singerPhotoPath"));
        musician.setEstablishDate(rs.getDate("establishDate"));
        musician.setBelongedCombinationId(rs.getInt("belongedCombinationid"));
        musician.setMusicSet(new HashSet<>());
        musician.setCombination((Combination) combinationDao.find(rs.getInt("belongedCombinationid")));
        return musician;
    }
}
